package com.sos.core.game.requests
{
	
	import com.sos.utils.ClassUtils;

	public class GameRequestResultCode
	{
			
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------              
		
		public static const UNKNOWN:GameRequestResultCode			= new GameRequestResultCode(9, 	"UNKNOWN",		"");	//
		public static const GAME_END:GameRequestResultCode			= new GameRequestResultCode(2, 	"GAME_END",		"");	// 
		public static const WAIT:GameRequestResultCode				= new GameRequestResultCode(1, 	"WAIT",			"");	// 
		public static const SUCCESS:GameRequestResultCode			= new GameRequestResultCode(0, 	"SUCCESS",		"");	// 
		public static const ERROR:GameRequestResultCode				= new GameRequestResultCode(-1, "ERROR",		"");	// 
		public static const EXCEPTION:GameRequestResultCode			= new GameRequestResultCode(-2, "EXCEPTION",	"");	// 
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameRequestResultCode>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameRequestResultCode>();
			_list.push(GameRequestResultCode.UNKNOWN);
			_list.push(GameRequestResultCode.GAME_END);
			_list.push(GameRequestResultCode.WAIT);
			_list.push(GameRequestResultCode.SUCCESS);
			_list.push(GameRequestResultCode.ERROR);
			_list.push(GameRequestResultCode.EXCEPTION);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:int;
		private var _name:String;
		private var _description:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameRequestResultCode(id:int, name:String, desc:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameRequestResultCode enum already created!");
			// Init
			_id    			= id;
			_name  			= name;
			_description	= desc;
		}

		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():int
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get description():String
		{
			return _description;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
				
		/**
		 * Search a GameRequestResultCode by given id 
		 * @param id Id of the card type to find
		 * @return PlayerGameRequestResultCode constant for the given id, null if no GameRequestResultCode
		 * for the given id has been found
		 */             
		public static function getById(id:int):GameRequestResultCode
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameRequestResultCode for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameRequestResultCode by given name 
		 * @param name Name of the player GameRequestResultCode to find
		 * @return PlayerGameRequestResultCode constant for the given id, null if no GameRequestResultCode
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameRequestResultCode
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameRequestResultCode for name '" + name + "' unknown!");
		}
			
	}
}